package org.jeecg.modules.app.login.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.login.service.AppLoginService;
import org.jeecg.modules.app.login.service.ProgramLoginService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.login.controller
 * @Author: mi
 * @CreateTime: 2024-07-09 15:40
 * @Description:
 */
@CrossOrigin
@Api(tags = "APP运营端登陆相关接口")
@RestController
@RequestMapping("/api/appLogin")
public class AppLoginController {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private AppLoginService appLoginService;
    @Autowired
    private ProgramLoginService programLoginService;


    @ApiOperation("APP运营端账号密码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true),
    })
    @PostMapping("/accountLogin")
    public Result<?> accountLogin(String account, String password){
        if (oConvertUtils.isEmpty(account))return Result.error_599("请填写密码账号");
        if (oConvertUtils.isEmpty(password))return Result.error_599("请填写密码");
        //校验是不是业务员
        SysUser sysUser = sysUserService.getUserByRoleIdApp("1811671800946208769",account);
        if (oConvertUtils.isEmpty(sysUser))return Result.error_599("账号不存在");
        if (CommonConstant.USER_FREEZE.equals(sysUser.getStatus())) return Result.error_599("用户已被冻结");
        if (CommonConstant.DEL_FLAG_1.equals(sysUser.getDelFlag())) return Result.error_599("用户已被注销");
        //判定密码
        //2. 校验用户名或密码是否正确
        String userpassword = PasswordUtil.encrypt(account, password, sysUser.getSalt());
        String syspassword = sysUser.getPassword();
        if (!syspassword.equals(userpassword))return Result.error_599("账号或密码错误");
        System.out.println("校验通过=="+sysUser);
        //执行登陆操作
        return appLoginService.accountLogin(sysUser);
    }

    //账号退出
    @ApiLogin
    @ApiOperation("APP运营端账号退出登陆")
    @PostMapping("appUserQuit")
    public Result<?>appUserQuit(HttpServletRequest request){
        String token = request.getHeader("token"); // 从 http 请求头中取出 token  token
        return  programLoginService.userQuit(token);
    }










}
